package com.pshcp.patient.tool;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.external.ws.soap.entity.dto.CreatPatCardDTO;
import com.external.ws.soap.entity.dto.PatientCheckDTO;
import com.external.ws.soap.entity.dto.UpdatePatInfoDTO;
import com.external.ws.soap.entity.res.CreatPatCardRes;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CardTypeEnum;
import com.pshcp.common.enums.MaritalStatusEnum;
import com.pshcp.common.enums.PatCardTypeEnum;
import com.pshcp.domain.model.Patient;
import com.pshcp.patient.domain.patient.ao.PatientAddAO;
import com.pshcp.patient.domain.patient.ao.PatientMedicalIntroAO;
import com.pshcp.patient.domain.patient.ao.PatientOnlineFilingAO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

@Slf4j
public class PatientTools {

    public static Patient buildAddAO2Model(PatientAddAO addAO){
        Patient patient = CommonTool.copyProperties(addAO, Patient.class);
        patient.setPatientUserId(ReqComHold.getReqCom().getUserId());
        if(addAO.getPatientId() != null){
            if(!ObjectUtils.isEmpty(addAO.getIdCardNo())
                    && addAO.getIdCardNo().contains("*")){
                patient.setIdCardNo(null);
            }
            if(!ObjectUtils.isEmpty(addAO.getPhone())
                    && addAO.getPhone().contains("*")){
                patient.setPhone(null);
            }
        }
        return patient;
    }

    public static Patient buildFilingAO2Model(PatientOnlineFilingAO filingAO){
        Patient patient = CommonTool.copyProperties(filingAO, Patient.class);
        patient.setPatientUserId(ReqComHold.getReqCom().getUserId());
        patient.setId(filingAO.getPatientId());
        patient.setPatientCardType(PatCardTypeEnum.PAT_VISIT_CARD.getCode());
        return patient;
    }

    public static Patient buildIntroAO2Model(PatientMedicalIntroAO introAO){
        Patient patient = CommonTool.copyProperties(introAO, Patient.class);
        patient.setId(introAO.getPatientId());
        return patient;
    }

    public static PatientCheckDTO buildWsCheckPatientDTO(Patient patient){
        PatientCheckDTO checkDTO = new PatientCheckDTO();
        checkDTO.setIdCardType(patient.getIdCardType()+"");
        checkDTO.setIdCardNo(patient.getIdCardNo());
        checkDTO.setPatCardNo(patient.getPatientCardNo());
        checkDTO.setPatCardType(patient.getPatientCardType()+"");
        checkDTO.setPatId(patient.getId());
        checkDTO.setPatName(patient.getName());
        return checkDTO;
    }

    public static CreatPatCardDTO buildWsCreatePatCardDTO(PatientOnlineFilingAO filingAO){
        CreatPatCardDTO dto = CommonTool.copyProperties(filingAO, CreatPatCardDTO.class);
        dto.setPatName(filingAO.getName());
        dto.setIdCardType(filingAO.getIdCardType()+"");
        dto.setPhoneNo(filingAO.getPhone());
        dto.setPostNo("");
        dto.setMaritalStatus("");
        dto.setOccupation("");
        dto.setWorkUnit("");
        dto.setWorkUnitAddr("");
        dto.setWorkUnitPhone("");
        dto.setRegisteredAddress("");
        dto.setBirthPlace("");
        dto.setNativePlace("");
        dto.setContactsIdCardNo("");
        dto.setContactsAddr("");
        if(ObjectUtils.isEmpty(dto.getContactsName())){
            dto.setContactsName("暂无");
        }
        return dto;
    }

    public static void buildWsCreatePatCardResDTO(Patient patient, CreatPatCardRes patCardRes){
        if(!ObjectUtils.isEmpty(patCardRes.getAccessPatId())){
            patient.setHisPatientId(patCardRes.getAccessPatId());
        }
        if(!ObjectUtils.isEmpty(patCardRes.getPatCardNo())){
            patient.setPatientCardNo(patCardRes.getPatCardNo());
        }
        if(!ObjectUtils.isEmpty(patCardRes.getPatCardType())){
            patient.setPatientCardType(patCardRes.getPatCardType());
        }
    }

    public static UpdatePatInfoDTO buildWsUpdatePatDTO(Patient patient, PatientMedicalIntroAO introAO){
        UpdatePatInfoDTO patInfoDTO = CommonTool.copyProperties(introAO, UpdatePatInfoDTO.class);
        patInfoDTO.setBirthday("");
        patInfoDTO.setSex("");
        patInfoDTO.setRegisteredAddress(introAO.getRegisteredAddress()+introAO.getRegisteredAddressDetail());
        if(patient.getIdCardType().intValue() == CardTypeEnum.NORMAL_ID.getCode()){
            patInfoDTO.setBirthday(getBirthDay(patient.getIdCardNo()));
            patInfoDTO.setSex(getSex(patient.getIdCardNo()));
        }
        patInfoDTO.setMaritalStatus(MaritalStatusEnum.getDescByCode(introAO.getMaritalStatus()));

        patInfoDTO.setAccessPatId(patient.getHisPatientId());
        patInfoDTO.setPhoneNo(patient.getPhone());
        patInfoDTO.setCurrentAddr(patient.getCurrentAddr());
        patInfoDTO.setFolk(patient.getFolk());
        patInfoDTO.setNationality(patient.getNationality());
        patInfoDTO.setContactsName(patient.getContactsName());
        patInfoDTO.setContactsPhoneNo(patient.getContactsPhoneNo());
        //set default val
        patInfoDTO.setWorkUnitAddr("无");
        patInfoDTO.setContactsIdCardNo("0");
        return patInfoDTO;
    }

    public static String getBirthDay(String cardNo){
        String birthDayStr = cardNo.substring(6, 14);
        DateTime dateTime = DateUtil.parse(birthDayStr, DatePattern.PURE_DATE_FORMAT);
        return DateUtil.formatDate(dateTime);
    }

    /**
     * 0女 1男
     * @param cardNo
     * @return
     */
    public static String getSex(String cardNo){
        try {
            Integer sex = Integer.valueOf(cardNo.substring(16, 17));
            return (sex % 2 == 0) ? "0" : "1";
        }catch (Exception e){
            log.error("get sex by card no fail", e);
        }
        return "";
    }

    public static int getAge(String cardNo){
        try {
            if (StringUtils.isBlank(cardNo)||cardNo.length()<14){
                return 0;
            }
            String birthDayStr = cardNo.substring(6, 14);
            DateTime dateTime = DateUtil.parse(birthDayStr, DatePattern.PURE_DATE_FORMAT);
            return DateUtil.ageOfNow(dateTime);
        }catch (Exception e){
            log.error("get age by card no fail", e);
        }
        return 0;
    }

    /**
     * 0女 1男
     * @param cardNo
     * @return
     */
    public static String getSexDesc(String cardNo){
        try {
            if (StringUtils.isBlank(cardNo)||cardNo.length()<17){
                return "";
            }
            Integer sex = Integer.valueOf(cardNo.substring(16, 17));
            return (sex % 2 == 0) ? "女" : "男";
        }catch (Exception e){
            log.error("get sex desc by card no fail", e);
        }
        return "";
    }

}
